package com.fanbai.score.user.fragemnt;

/**
 * Created by acer on 2017/5/6.
 */

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbai.score.R;
import com.fanbai.score.common.Utils.ImageUtils;
import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.Utils.SPUtils;
import com.fanbai.score.common.Utils.ToastUtils;
import com.fanbai.score.common.activity.WebView_Activity;
import com.fanbai.score.common.base.BaseFragment;
import com.fanbai.score.common.activity.CommonList_Activity;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.view.CircleImageView;
import com.fanbai.score.user.activity.Setting_Activity;
import com.fanbai.score.user.activity.UserInfo_Activity;
import com.fanbai.score.user.bean.User_Bean;
import com.fanbai.score.user.http.UserRequestNetwork_Utils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A placeholder fragment containing a simple view.
 */
public class MyInfo_Fragment extends BaseFragment {

    @BindView(R.id.userFragmentInfo_img)
    CircleImageView img;
    @BindView(R.id.userFragmentInfo_name)
    TextView nameText;
    @BindView(R.id.userFragmentInfo_score)
    TextView scoreText;
    @BindView(R.id.userFragmentInfo_blecoin)
    TextView blecionText;

    @OnClick({R.id.userFragmentInfo_cashLayout,
            R.id.userFragmentInfo_userInfo,
            R.id.userFragmentInfo_consumptionLayout,
            R.id.userFragmentInfo_elegantLayout,
            R.id.userFragmentInfo_redLayout,
            R.id.userFragmentInfo_goodsLayout,
            R.id.userFragmentInfo_shareLayout,
            R.id.userFragmentInfo_couponLayout,
            R.id.userFragmentInfo_helpLayout,
    R.id.userFragmentInfo_integralLayout,
    R.id.userFragmentInfo_settingLayout})
    public void onClick(View view){
        Bundle bundle;
        switch (view.getId()){
            case R.id.userFragmentInfo_cashLayout://现金券
                bundle = new Bundle();
                bundle.putString("url", "http://score.xinyou777.cn/customer/record/usercashcouponuseinfos.php");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_userInfo://用户详情
                ctx.jumpToActivity(UserInfo_Activity.class);
                break;
            case R.id.userFragmentInfo_consumptionLayout://消费记录
                /*bundle = new Bundle();
                bundle.putString("status", "xiaofei");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);*/
                bundle = new Bundle();
                bundle.putString("url",
                        "http://score.xinyou777.cn/customer/record/orderlist.php");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_elegantLayout://贵人币记录
                /*bundle = new Bundle();
                bundle.putString("status", "guirenbi");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);*/
                bundle = new Bundle();
                bundle.putString("url",
                        "http://score.xinyou777.cn/customer/record/coinexchargelist.php");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_redLayout://红包兑换记录
                /*bundle = new Bundle();
                bundle.putString("status", "hongbao");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);*/
                bundle = new Bundle();
                bundle.putString("url",
                        "http://score.xinyou777.cn/customer/record/redbagexchargelist.php");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_goodsLayout://商品兑换记录
                /*bundle = new Bundle();
                bundle.putString("status", "shangpin");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);*/

                break;
            case R.id.userFragmentInfo_shareLayout://分享
                bundle = new Bundle();
                bundle.putString("url",
                        "http://score.xinyou777.cn/customer/html/share.html");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_helpLayout://帮助
                bundle = new Bundle();
                bundle.putString("url", "http://score.xinyou777.cn/customer/html/help.html");
                ctx.jumpToActivity(WebView_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_couponLayout://活动券
                bundle = new Bundle();
                bundle.putString("status", "huodongquan");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_integralLayout://积分记录
                bundle = new Bundle();
                bundle.putString("status", "jifen");
                ctx.jumpToActivity(CommonList_Activity.class, bundle);
                break;
            case R.id.userFragmentInfo_settingLayout://设置
                ctx.jumpToActivity(Setting_Activity.class);
                break;
        }
    }

    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static MyInfo_Fragment newInstance(int sectionNumber) {
        MyInfo_Fragment fragment = new MyInfo_Fragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    public MyInfo_Fragment() {
    }


    @Override
    protected int getLayoutID() {
        return R.layout.user_fragment_info;
    }

    @Override
    public String getTitleName() {
        return "设置";
    }

    @Override
    public void iniTitleLeft(ImageView imageView) {
    }

    @Override
    public void iniTitleRight(TextView tv) {

    }

    @Override
    protected void initView(View view) {
    }

    @Override
    protected void initData() {
        getUserInfo();
    }

    @Override
    public void onResume() {
        super.onResume();
        User_Bean bean = JsonUtils.toBean(SPUtils.with(ctx).get(SPUtils.KEY_USER_USERINFO, ""), User_Bean.class);
        setUserData(bean);
    }

    private void setUserData(User_Bean bean){
        ImageUtils.show(ctx, bean.getClogoimage(), img, R.mipmap.ic_launcher);
        nameText.setText(TextUtils.isEmpty(bean.getCnickname())? bean.getCmobile(): bean.getCnickname());
        scoreText.setText("我的积分："+SPUtils.with(ctx).get(SPUtils.KEY_USER_TOTALSCORE, "0"));
        blecionText.setText("我的贵人币："+SPUtils.with(ctx).get(SPUtils.KEY_USER_RECHARGABLECOIN, "0"));
    }

    @Override
    protected void initListener() {

    }

    private void getUserInfo(){
        UserRequestNetwork_Utils.getUserInfo(ctx, new CallBack<User_Bean>() {
            @Override
            public void onSuccess(String code, String msg, User_Bean bean) {
                SPUtils.with(ctx).save(SPUtils.KEY_USER_USERINFO, JsonUtils.toJsonObj(bean));
                setUserData(bean);
            }

            @Override
            public void onFailure(String code, String msg, String body) {
                ToastUtils.WarnImageToast(ctx, msg);
            }

            @Override
            public void onError(String code, String msg) {
                ToastUtils.WarnImageToast(ctx, msg);
            }
        });
    }

}
